package com.shujia.api.dao;

import com.shujia.api.bean.StationDayTop;
import com.shujia.api.util.JdbcUtil;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * 数据库访问层，将查询数据库的代码放到这个层
 */

@Component
public class SztDao {
    public ArrayList<StationDayTop> queryInStationDayTopByDay(String day) throws Exception {
        System.out.println("数据库访问层");
        ArrayList<StationDayTop> stationDayTops = new ArrayList<>();

        //从连接池获取链接
        Connection con = JdbcUtil.getConnection();

        //2、查询数据
        PreparedStatement stat = con.prepareStatement("select * from ads_in_station_day_top where day=?");
        stat.setString(1, day);

        //3、执行插叙
        ResultSet resultSet = stat.executeQuery();

        //4、解析数据
        while (resultSet.next()) {
            String station = resultSet.getString("station");
            long count = resultSet.getLong("count");

            StationDayTop stationDayTop = new StationDayTop(station, count, day);


            stationDayTops.add(stationDayTop);
        }

        //放回链接
        con.close();

        return stationDayTops;

    }

}
